package com.audaque.springboot.foshanupload.web.webdemo.controller;

import cn.hutool.core.img.ImgUtil;
import com.audaque.springboot.foshanupload.core.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-24 18:22:58
 */
@Slf4j
@RestController
@RequestMapping("image")
public class ImageController {


    /**
     * 根据url输出图片到response body
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String url) {
        response.setContentType("image/*");
        response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
        response.setHeader("Cache-Control", "no-cache");
        InputStream fis = null;
        OutputStream os = null;
        try {
            URL getUrl = new URL(url);
            URLConnection connection = getUrl.openConnection();
            connection.setRequestProperty("Referer", "");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            connection.setDoOutput(true);
            fis = connection.getInputStream();
            os = response.getOutputStream();
            int count = 0;
            //缓冲区
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                //输出到response body
                os.write(buffer, 0, count);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /**
     * 根据url获取图片字节数组
     */
    @GetMapping("/url2img2bytes")
    public ByteArrayResource url2img2bytes(HttpServletResponse response,@RequestParam String url) {
        response.setContentType("image/*");

        InputStream fis = null;
        //关键
        ByteArrayOutputStream os = null;
        try {
            URL getUrl = new URL(url);
            URLConnection connection = getUrl.openConnection();
            connection.setRequestProperty("Referer", "");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36");
            connection.setDoOutput(true);
            fis = connection.getInputStream();
            os =  new ByteArrayOutputStream();
            int count = 0;
            //缓冲区
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                //输出到response body
                os.write(buffer, 0, count);
            }
            ByteArrayResource resource = new ByteArrayResource(os.toByteArray());
            return resource;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }



}
